﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Hfm.Common.Entity.Contacts;
using Hfm.Common.Constants.Customer;

namespace Hfm.Core.DataAccess.Customer
{
    public class ContactsData
    {
        public readonly CoreContext Context;

        public ContactsData()
        {
            
        }

        public ContactsData(CoreContext context)
        {
            Context = context;
        }
        /// <summary>
        /// 得到联系人
        /// </summary>
        /// <returns></returns>
        public virtual IList<NewContactsEntity> GetContacts()
        {
            var query = from c in Context.ContactsEntities
                select new NewContactsEntity
                {
                    ID = c == null ? 0 : c.ID,
                    FullName = c == null ? string.Empty : c.FullName,
                    OfficeNumber = c == null ? string.Empty : c.OfficeNumber,
                    Mobile = c == null ? string.Empty : c.Mobile,
                    FaxNumber = c == null ? string.Empty : c.FaxNumber,
                    Email = c == null ? string.Empty : c.Email,
                    Designation = c == null ? string.Empty : c.Designation,
                    SalutationID = c == null ? 0 : c.SalutationID,
                    Department = c == null ? string.Empty : c.Department,
                    Remarks = c == null ? string.Empty : c.Remarks,
                    CID = c == null ? 0 : c.CID
                };

            query = query.OrderBy(c => c.ID);

            IList<NewContactsEntity> result = query.ToList();

            foreach (var item in result)
            {
                item.Salutation = CustomerConstants.Salutation[item.SalutationID];
            }

            return result;
        }
        /// <summary>
        /// 通过ID得到联系人
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual NewContactsEntity GetContactsByID(int ID)
        {
            var query = from c in Context.ContactsEntities
                        where c.ID==ID
                        select new NewContactsEntity
                        {
                            ID = c == null ? 0 : c.ID,
                            FullName = c == null ? string.Empty : c.FullName,
                            OfficeNumber = c == null ? string.Empty : c.OfficeNumber,
                            Mobile = c == null ? string.Empty : c.Mobile,
                            FaxNumber = c == null ? string.Empty : c.FaxNumber,
                            Email = c == null ? string.Empty : c.Email,
                            Designation = c == null ? string.Empty : c.Designation,
                            SalutationID = c == null ? 0 : c.SalutationID,
                            Department = c == null ? string.Empty : c.Department,
                            Remarks = c == null ? string.Empty : c.Remarks,
                            CID = c == null ? 0 : c.CID
                        };

            query = query.OrderBy(c => c.ID);

            NewContactsEntity result = query.SingleOrDefault();
            result.Salutation = CustomerConstants.Salutation[result.SalutationID];

            return result;
        }
        /// <summary>
        /// 根据顾客ID查找联系人
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        public virtual IList<NewContactsEntity> GetContactsByCID(int CID)
        {
            var query = from c in Context.ContactsEntities
                        where c.CID==CID
                        select new NewContactsEntity
                        {
                            ID = c == null ? 0 : c.ID,
                            FullName = c == null ? string.Empty : c.FullName,
                            OfficeNumber = c == null ? string.Empty : c.OfficeNumber,
                            Mobile = c == null ? string.Empty : c.Mobile,
                            FaxNumber = c == null ? string.Empty : c.FaxNumber,
                            Email = c == null ? string.Empty : c.Email,
                            Designation = c == null ? string.Empty : c.Designation,
                            SalutationID = c == null ? 0 : c.SalutationID,
                            Department = c == null ? string.Empty : c.Department,
                            Remarks = c == null ? string.Empty : c.Remarks,
                            CID = c == null ? 0 : c.CID
                        };

            query = query.OrderBy(c => c.ID);

            IList<NewContactsEntity> result = query.ToList();

            foreach (var item in result)
            {
                item.Salutation = CustomerConstants.Salutation[item.SalutationID];
            }

            return result; ;
        }
        /// <summary>
        /// 根据顾客ID查找联系人
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        public virtual IList<ContactsEntity> GetContactsByCustomerID(int CID)
        {
            var query = from c in Context.ContactsEntities
                where c.CID == CID
                select c;
            return query.ToList();
        }
        /// <summary>
        /// 创建联系人
        /// </summary>
        /// <param name="Contacts"></param>
        public virtual void CreateContacts(ContactsEntity Contacts)
        {
            Context.ContactsEntities.Add(Contacts);
            Context.SaveChanges();
        }
        /// <summary>
        /// 更新联系人
        /// </summary>
        /// <param name="Contacts"></param>
        public virtual void UpdateContacts(ContactsEntity Contacts)
        {
            Context.ContactsEntities.Attach(Contacts);
            Context.Entry(Contacts).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }
        /// <summary>
        /// 删除联系人
        /// </summary>
        /// <param name="ID"></param>
        public virtual void DeleteContacts(int ID)
        {
            string strSql = "DELETE FROM Contacts WHERE ID = @ID ";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@ID", ID));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }
        /// <summary>
        /// 根据顾客删除联系人
        /// </summary>
        /// <param name="ID"></param>
        public virtual void DeleteContactsByCID(int CID)
        {
            string strSql = "DELETE FROM Contacts WHERE CID = @CID ";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@CID", CID));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }
    }
}
